package com.kgc.skProvider.service.serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.SalesReceivable;
import com.kgc.skProvider.dao.SalesReceivableMapper;
import com.kgc.skProvider.service.SalesReceivableService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SalesReceivableServiceImpl implements SalesReceivableService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SalesReceivableMapper salesReceivableMapper;

    @Override
    public ResponseMsg selectSalesReceivableByPage(Map<String, Object> map) {
        logger.info("SalesReceivableServiceImpl selectSalesReceivableByPage params is{}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        PageInfo<SalesReceivable> pageInfo = new PageInfo<>();
        pageInfo.setPageNo(map.get("pageNo")==null?1:Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize")==null?10:Integer.parseInt(map.get("pageSize").toString()));
        Page page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize());
        List<SalesReceivable> list = salesReceivableMapper.selectSalesReceivableByPage(map);
        pageInfo.setList(list);
        pageInfo.setTotal((int)page.getTotal());
        responseMsg.setData(pageInfo);
        logger.debug("SalesReceivableServiceImpl selectSalesReceivableByPage success");
        return responseMsg;
    }

    @Override
    public ResponseMsg insertSalesReceivable(SalesReceivable salesReceivable) {
        logger.info("SalesReceivableServiceImpl insertSalesReceivable is{}",salesReceivable.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        int result = salesReceivableMapper.insertSalesReceivable(salesReceivable);
        if(result>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesReceivableServiceImpl insertSalesReceivable success");
        return responseMsg;
    }

    @Override
    public ResponseMsg updateSalesReceivable(SalesReceivable salesReceivable) {
        logger.info("SalesReceivableServiceImpl updateSalesReceivable is{}",salesReceivable.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        int result = salesReceivableMapper.updateSalesReceivable(salesReceivable);
        if(result>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesReceivableServiceImpl updateSalesReceivable success");
        return responseMsg;
    }

    @Override
    public ResponseMsg deleteSalesReceivable(Map<String, Object> map) {
        logger.info("SalesReceivableServiceImpl deleteSalesReceivable is{}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        int result = salesReceivableMapper.deleteSalesReceivable(map);
        if(result>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesReceivableServiceImpl deleteSalesReceivable success {}",responseMsg);
        return responseMsg;
    }
}
